library(foreign)
library(readstata13)
library(collapse)
library(sandwich)
library(lmtest)
library(pscl)
library(arm)
library(ggeffects)

setwd("/Users/joelsievert/Dropbox/Research/Legislative Responsiveness/National Road/data/PRQ Replication")


####################################
##votes to transfer road to states##
###################################

h17 <- read.csv("h17_transfer.csv")
h20 <- read.csv("h20_transfer.csv")
h23 <- read.csv("h23_transfer.csv")

#17th
m1 <- glm(rc ~ dem_rep + slave_p  + dist + distrib_cmte, family = binomial, data = h17)
m2 <- glm(rc ~  slave_p + dist + distrib_cmte, family = binomial, data = subset(h17, dem_rep == 1))

o1 <- coeftest(m1, vcov = vcovHC(m1, "HC0")) 
o2 <- coeftest(m2, vcov = vcovHC(m2, "HC0")) 

p1 <- ggpredict(m1, terms = "dem_rep", condition = c(dist = 2.8, slave_p = 0, distrib_cmte = 0) )
p2 <- ggpredict(m1, terms = "distrib_cmte", condition = c(dem_rep = 1, dist = 2.8, slave_p = 0))
p3 <- ggpredict(m2, terms = "distrib_cmte", condition = c( dist = 2.8, slave_p = 0) ) 


#20th congress
m3 <- glm(rc ~ jackson + dist + slave_p  +  distrib_cmte , family = binomial, data = h20)
m4 <- glm(rc ~ dist+ slave_p + distrib_cmte , family = binomial, data = subset(h20, jackson == 1))

o3 <- coeftest(m3, vcov = vcovHC(m3, "HC0")) 
o4 <- coeftest(m4, vcov = vcovHC(m4, "HC0")) 

p1 <- ggpredict(m3, terms = "jackson", condition = c(dist = 2.5, slave_p = 0, distrib_cmte = 0) ) 


#23rd

m5 <-glm(rc ~ jackson  + dist+ slave_p  + distrib_cmte , family = binomial, data = h23)
m6 <- glm(rc ~   dist+ slave_p + distrib_cmte   , family = binomial, data = subset(h23, jackson == 1))

o5 <- coeftest(m5, vcov = vcovHC(m5, "HC0")) 
o6 <- coeftest(m6, vcov = vcovHC(m6, "HC0")) 



p1 <- ggpredict(m5, terms = "jackson", condition = c(dist = 2.4, slave_p = 0, distrib_cmte = 0) )
p2 <- ggpredict(m5, terms = "distrib_cmte", condition = c(jackson = 1, dist = 2.4, slave_p = 0) )



stargazer(o1, o2, o3, o4, o5, o6, digits = 2)


#calculate PRE
#See code from Dave Armstrong
pre(m1)
pre(m2)
pre(m3)
pre(m4)
pre(m5)
pre(m6)

